import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from "../views/Login";
import Register from "../views/Register";
import Main from "../views/Main";
import SalesModel from "../components/SalesModel";
import CommoditieModel from "../components/CommoditieModel";
import AccessoryModel from "../components/AccessoryModel";
import ContractModel from "../components/ContractModel";
import Biao from "../components/Biao";
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    name:'Login',
    component:Login
  },
  {
    path:'/login',
    name:'Login',
    component:Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
  },
  {
    path:'/main',
    name:Main,
    component:Main,
    children:[
      {
        path:'/main/salesModel',
        name:SalesModel,
        component:SalesModel
      },
      {
        path:'/main/',
        name:SalesModel,
        component:SalesModel
      },
      {
        path:'/main/commodities',
        name: CommoditieModel,
        component:CommoditieModel
      },
      {
        path:'/main/accessory/:id',
        name: AccessoryModel,
        component:AccessoryModel
      },
      {
        path:'/main/contract',
        name:ContractModel,
        component:ContractModel
      },
      {
        path:'/main/biao/:id',
        name:Biao,
        component:Biao

      }
    ]
  },

  // {
  //   path: '/',
  //   name: 'Home',
  //   component: Home
  // },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register' || to.path.includes('/index') || to.path === '/') {
    return next()
  } else {
    const username =localStorage.getItem('userInfo')
    if (!username) {
      return next('/login')
    } else {
      return next()
    }
  }
})
export default router
